package com.cityfood.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.pojo.CfAdvertising;
import com.cityfood.service.ADService;
import com.github.pagehelper.PageInfo;

@Controller
public class ADController {
	
	@Autowired
	private ADService adService;
	
	/**
	 * 查询首页的banner图片
	 */
	@RequestMapping(value="/banner",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getbanner(){
		CityFoodResult cityfoodResult = new CityFoodResult();
		List<CfAdvertising> list = adService.getADBySite("banner");
		if (list.size()>0) {
			
			List<Map<String,Object>> ls = new ArrayList<>();
			for (CfAdvertising ad : list) {
				if (ad.getIsdisplay()!=0) {
					
					Map<String,Object> ms = new LinkedHashMap<>();
					ms.put("id", ad.getId());
					ms.put("ADpath", ad.getAdPath());
					ms.put("ADsite", ad.getAdSite());
					ms.put("ADtype", ad.getAdType());
					ls.add(ms);
				}
			}
			
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("查询banner成功");
			cityfoodResult.setData(ls);
			return cityfoodResult;
		}
		cityfoodResult.setCode(201);
		cityfoodResult.setMsg("暂无广告信息");
		cityfoodResult.setData("{}");
		return cityfoodResult;
	}
	
	/**
	 * 根据广告类型查询广告信息
	 */
	@RequestMapping(value="/manage/ad",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getADBySite(@RequestParam(defaultValue="")String site,
									  @RequestParam(defaultValue="1")Integer currentPage){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		PageInfo<CfAdvertising> list = adService.getADBySiteAndPage(site, currentPage);
		if (list!=null) {
			if (list.getList().size()==0) {
				cityfoodResult.setCode(201);
				cityfoodResult.setMsg("暂无广告信息");
				cityfoodResult.setData("{}");
				return cityfoodResult;
			}
			Map<String,Object> mp = new LinkedHashMap<>();
			//存入查询到的总数
			mp.put("total", list.getTotal());
			//存入需要展示的数据
			mp.put("ads", list.getList());
			
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取广告列表成功");
			cityfoodResult.setData(mp);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无广告信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
	}
	
	/**
	 * 添加广告信息
	 */
	@RequestMapping(value="/manage/ad",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertAD(@RequestBody Map<String,Object> map){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		String adPath = (String)map.get("adPath");
		String adSite = (String)map.get("adSite");
		String adType = (String)map.get("adType");
		
		CfAdvertising advertising=new CfAdvertising();
		advertising.setAdPath(adPath);
		advertising.setAdSite(adSite);
		advertising.setAdType(adType);
		
		int i = adService.insertAD(advertising);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("添加广告成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("添加广告失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	/**
	 * 修改广告信息
	 */
	@RequestMapping(value="/manage/ad",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateAD(@RequestBody Map<String,Object> map){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		Integer id = Integer.parseInt(map.get("id").toString());
		String adPath = (String)map.get("adPath");
		String adSite = (String)map.get("adSite");
		String adType = (String)map.get("adType");
		
		
		CfAdvertising advertising=new CfAdvertising();
		advertising.setId(id);
		advertising.setAdPath(adPath);
		advertising.setAdSite(adSite);
		advertising.setAdType(adType);
		
		int i = adService.updateAD(advertising);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("修改广告成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("修改广告失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	/**
	 * 删除广告信息
	 */
	@RequestMapping(value="/manage/ad/remove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deletetAD(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfAdvertising advertising=new CfAdvertising();
		advertising.setId(Integer.parseInt(id));
		int i = adService.deletetAD(advertising);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("删除广告成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("删除广告失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	/**
	 * 删除广告信息
	 */
	@RequestMapping(value="/manage/ad/batchremove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deletetADs(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			CfAdvertising advertising=new CfAdvertising();
			advertising.setId(Integer.parseInt(strId));
			int i = adService.deletetAD(advertising);
			if (i>0) {
				cityfoodResult.setCode(200);
				cityfoodResult.setMsg("删除广告成功");
				cityfoodResult.setData(true);
			}else{
				cityfoodResult.setCode(201);
				cityfoodResult.setMsg("删除广告失败");
				cityfoodResult.setData(false);
				return cityfoodResult;
			}
		}
		return cityfoodResult;
	}
	

}
